from hashlib import md5
import datetime

from flask_peewee.auth import BaseUser
from peewee import *
import config
import peewee
from app import db


class User(db.Model, BaseUser):
    username = CharField()
    password = CharField()
    email = CharField()
    join_date = DateTimeField(default=datetime.datetime.now)
    active = BooleanField(default=True)
    admin = BooleanField(default=False)

    def __unicode__(self):
        return self.username

    def following(self):
        return User.select().join(
            Relationship, on='to_user_id'
        ).where(from_user=self).order_by('username')

    def followers(self):
        return User.select().join(
            Relationship, on='from_user_id'
        ).where(to_user=self).order_by('username')

    def is_following(self, user):
        return Relationship.filter(
            from_user=self,
            to_user=user
        ).exists()

    def gravatar_url(self, size=80):
        return 'http://www.gravatar.com/avatar/%s?d=identicon&s=%d' % \
            (md5(self.email.strip().lower().encode('utf-8')).hexdigest(), size)


class Relationship(db.Model):
    from_user = ForeignKeyField(User, related_name='relationships')
    to_user = ForeignKeyField(User, related_name='related_to')
    
    def __unicode__(self):
        return 'Relationship from %s to %s' % (self.from_user, self.to_user)


class Message(db.Model):
    user = ForeignKeyField(User)
    content = TextField()
    pub_date = DateTimeField(default=datetime.datetime.now)
    
    def __unicode__(self):
        return '%s: %s' % (self.user, self.content)

class Note(db.Model):
    user = ForeignKeyField(User)
    message = TextField()
    created_date = DateTimeField(default=datetime.datetime.now)

class Catalog(db.Model):
    """
    type of book
    """
    type_nam = CharField()
    create_date = DateTimeField(default=datetime.datetime.now)
    active = BooleanField(default=True)
    content = TextField()

class Book(db.Model):
    """
    book define
    """
    book_name = CharField()
    book_type = ForeignKeyField(Catalog)
    publish_date = DateTimeField()
    author = CharField()
    qrc_code = CharField()
    current_status = IntegerField() #"0:LEND,1:LOSE,2:CONTRIBUTION,3:INVALID"
    shared_type = IntegerField()    #"0:buy from company,1:user shared,3:"
    shared_user = ForeignKeyField(User)
    lend_interver = IntegerField() #0:banyue,1.moonth 2,two moonth 3,three moonth
    image =

class MarkList(db.Model):
    """
    Mark list
    """
    user = ForeignKeyField(User)
    book = ForeignKeyField(Book)
    marked_date = DateTimeField()

class MessageBoard(db.Model):
    """
    MessageBoard
    """
    user = ForeignKeyField(User)
    content = ForeignKeyField(Book)
    create_date = DateTimeField()

class LendList(db.Model):
    """"
    type of book
    """
    user = ForeignKeyField(User)
    book = ForeignKeyField(Book)
    lend_date = DateTimeField(default=datetime.datetime.now)
    return_date = DateTimeField()
    active = BooleanField(default=True)
    type_name = CharField()
    content = TextField()

if __name__ == "__main__":
    custom_db = peewee.SqliteDatabase("example.db")
    User.create_table()
    Relationship.create_table()
    Message.create_table()
    Book.create_table()
    MarkList.create_table()
    Catalog.create_table()
    LendList.create_table()
    Note.create_table()
    MessageBoard.create_table()